home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Files / MoreFiles 1.4.3 / Sources / FSpCompat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-26  |  25.6 KB  |  914 lines  |  [TEXT/MPS ]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    FSSpec compatibility functions.
  5. **
  6. **    by Jim Luther, Apple Developer Technical Support Emeritus
  7. **
  8. **    File:        FSpCompat.c
  9. **
  10. **    Copyright © 1992-1996 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. /*
  23. **    If building application 68K code, set GENERATENODATA to 0 for faster code.
  24. **    If building stand-alone 68K code, set GENERATENODATA to 1 so globals
  25. **        (static variables) are not used.
  26. */
  27. #define    GENERATENODATA 0
  28.  
  29. #include <Types.h>
  30. #include <Errors.h>
  31. #include <LowMem.h>
  32. #include <Gestalt.h>
  33. #include <Resources.h>
  34. #include <Script.h>
  35.  
  36. #define    __COMPILINGMOREFILES
  37.  
  38. #include "MoreFilesExtras.h"
  39. #include "FSpCompat.h"
  40.  
  41. /*****************************************************************************/
  42.  
  43. /* local constants */
  44.  
  45. enum {
  46.     gestaltBugFixAttrsTwo                    = 'bugy',
  47.     gestaltFSpExchangeFilesCompatibilityFix    = 26,
  48.     gestaltBugFixAttrsThree                    = 'bugx',
  49.     gestaltFSpCreateScriptSupportFix        = 1
  50. };
  51.  
  52. /*****************************************************************************/
  53.  
  54. /* static prototypes */
  55.  
  56.  
  57. #if !SystemSevenOrLater
  58. static    Boolean    FSHasFSSpecCalls(void);
  59.  
  60. static    Boolean    QTHasFSSpecCalls(void);
  61. #endif    /* !SystemSevenOrLater */
  62.  
  63. #if !SystemSevenFiveOrLater
  64. static    Boolean    HasFSpExchangeFilesCompatibilityFix(void);
  65. #endif    /* !SystemSevenFiveOrLater */
  66.  
  67. static    Boolean    HasFSpCreateScriptSupportFix(void);
  68.  
  69. #if !SystemSevenFiveOrLater
  70. static    OSErr    GenerateUniqueName(short volume,
  71.                                    long *startSeed,
  72.                                    long dir1,
  73.                                    long dir2,
  74.                                    StringPtr uniqueName);
  75. #endif    /* !SystemSevenFiveOrLater */
  76.  
  77. /*****************************************************************************/
  78.  
  79. /* FSHasFSSpecCalls returns true if the file system provides FSSpec calls. */
  80.  
  81. #if !SystemSevenOrLater
  82. static    Boolean    FSHasFSSpecCalls(void)
  83. {
  84.     long            response;
  85. #if !GENERATENODATA
  86.     static Boolean    tested = false;
  87.     static Boolean    result = false;
  88. #else
  89.     Boolean    result = false;
  90. #endif
  91.     
  92. #if !GENERATENODATA
  93.     if ( !tested )
  94.     {
  95.         tested = true;
  96. #endif
  97.         if ( Gestalt(gestaltFSAttr, &response) == noErr )
  98.         {
  99.             result = ((response & (1L << gestaltHasFSSpecCalls)) != 0);
  100.         }
  101. #if !GENERATENODATA
  102.     }
  103. #endif
  104.     return ( result );
  105. }
  106. #endif    /* !SystemSevenOrLater */
  107.  
  108. /*****************************************************************************/
  109.  
  110. /* QTHasFSSpecCalls returns true if QuickTime provides FSSpec calls */
  111. /* except for FSpExchangeFiles. */
  112.  
  113. #if !SystemSevenOrLater
  114. static    Boolean    QTHasFSSpecCalls(void)
  115. {
  116.     long            response;
  117. #if !GENERATENODATA
  118.     static Boolean    tested = false;
  119.     static Boolean    result = false;
  120. #else
  121.     Boolean    result = false;
  122. #endif
  123.     
  124. #if !GENERATENODATA
  125.     if ( !tested )
  126.     {
  127.         tested = true;
  128. #endif
  129.         result = (Gestalt(gestaltQuickTimeVersion, &response) == noErr);
  130. #if !GENERATENODATA
  131.     }
  132. #endif
  133.     return ( result );
  134. }
  135. #endif    /* !SystemSevenOrLater */
  136.  
  137. /*****************************************************************************/
  138.  
  139. /* HasFSpExchangeFilesCompatibilityFix returns true if FSpExchangeFiles */
  140. /* compatibility code has been fixed in system software. */
  141. /* This was fixed by System Update 3.0, so if SystemSevenFiveOrLater */
  142. /* is true, then we know the fix is in. */
  143.  
  144. #if !SystemSevenFiveOrLater
  145. static    Boolean    HasFSpExchangeFilesCompatibilityFix(void)
  146. {
  147.     long            response;
  148. #if !GENERATENODATA
  149.     static Boolean    tested = false;
  150.     static Boolean    result = false;
  151. #else    /* !GENERATENODATA */
  152.     Boolean    result = false;
  153. #endif    /* !GENERATENODATA */
  154.     
  155. #if !GENERATENODATA
  156.     if ( !tested )
  157.     {
  158.         tested = true;
  159. #endif    /* !GENERATENODATA */
  160.         if ( Gestalt(gestaltBugFixAttrsTwo, &response) == noErr )
  161.         {
  162.             result = ((response & (1L << gestaltFSpExchangeFilesCompatibilityFix)) != 0);
  163.         }
  164. #if !GENERATENODATA
  165.     }
  166. #endif    /* !GENERATENODATA */
  167.     return ( result );
  168. }
  169. #endif    /* !SystemSevenFiveOrLater */
  170.  
  171. /*****************************************************************************/
  172.  
  173. /* HasFSpCreateScriptSupportFix returns true if FSpCreate and */
  174. /* FSpCreateResFile have been fixed in system software to correctly set */
  175. /* the scriptCode in the volume's catalog. */
  176. /* This was fixed by System 7.5 Update 1.0 */
  177.  
  178. static    Boolean    HasFSpCreateScriptSupportFix(void)
  179. {
  180.     long            response;
  181. #if !GENERATENODATA
  182.     static Boolean    tested = false;
  183.     static Boolean    result = false;
  184. #else
  185.     Boolean    result = false;
  186. #endif    /* !GENERATENODATA */
  187.     
  188. #if !GENERATENODATA
  189.     if ( !tested )
  190.     {
  191.         tested = true;
  192. #endif    /* !GENERATENODATA */
  193.         if ( Gestalt(gestaltBugFixAttrsThree, &response) == noErr )
  194.         {
  195.             result = ((response & (1L << gestaltFSpCreateScriptSupportFix)) != 0);
  196.         }
  197. #if !GENERATENODATA
  198.     }
  199. #endif    /* !GENERATENODATA */
  200.     return ( result );
  201. }
  202.  
  203. /*****************************************************************************/
  204.  
  205. /*
  206. **    File Manager FSp calls
  207. */
  208.  
  209. /*****************************************************************************/
  210.  
  211. pascal    OSErr    FSMakeFSSpecCompat(short vRefNum,
  212.                                    long dirID,
  213.                                    ConstStr255Param fileName,
  214.                                    FSSpecPtr spec)
  215. {
  216.     OSErr    result;
  217.     
  218. #if !SystemSevenOrLater
  219.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  220. #endif    /* !SystemSevenOrLater */
  221.     {
  222.         /* Let the file system create the FSSpec if it can since it does the job */
  223.         /* much more efficiently than I can. */
  224.         result = FSMakeFSSpec(vRefNum, dirID, fileName, spec);
  225.         /* Fix a bug in Macintosh PC Exchange's MakeFSSpec code where 0 is */
  226.         /* returned in the parID field when making an FSSpec to the volume's */
  227.         /* root directory by passing a full pathname in MakeFSSpec's */
  228.         /* fileName parameter. */
  229.         if ( (result == noErr) && (spec->parID == 0) )
  230.             spec->parID = fsRtParID;
  231.     }
  232. #if !SystemSevenOrLater
  233.     else
  234.     {
  235.         Boolean    isDirectory;
  236.         
  237.         result = GetObjectLocation(vRefNum, dirID, (StringPtr)fileName,
  238.                                     &(spec->vRefNum), &(spec->parID), spec->name,
  239.                                     &isDirectory);
  240.     }
  241. #endif    /* !SystemSevenOrLater */
  242.     return ( result );
  243. }
  244.  
  245. /*****************************************************************************/
  246.  
  247. pascal    OSErr    FSpOpenDFCompat(const FSSpec *spec,
  248.                                 char permission,
  249.                                 short *refNum)
  250. {
  251. #if !SystemSevenOrLater
  252.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  253. #endif    /* !SystemSevenOrLater */
  254.     {
  255.         return ( FSpOpenDF(spec, permission, refNum) );
  256.     }
  257. #if !SystemSevenOrLater
  258.     else
  259.     {
  260.         OSErr            result;
  261.         HParamBlockRec    pb;
  262.         
  263.         pb.ioParam.ioVRefNum = spec->vRefNum;
  264.         pb.fileParam.ioDirID = spec->parID;
  265.         pb.ioParam.ioNamePtr = (StringPtr) &(spec->name);
  266.         pb.ioParam.ioVersNum = 0;
  267.         pb.ioParam.ioPermssn = permission;
  268.         pb.ioParam.ioMisc = NULL;
  269.         result = PBHOpenSync(&pb);    /* OpenDF not supported by System 6, so use Open */
  270.         *refNum = pb.ioParam.ioRefNum;
  271.         return ( result );
  272.     }
  273. #endif    /* !SystemSevenOrLater */
  274. }
  275.  
  276. /*****************************************************************************/
  277.  
  278. pascal    OSErr    FSpOpenRFCompat(const FSSpec *spec,
  279.                                 char permission,
  280.                                 short *refNum)
  281. {
  282. #if !SystemSevenOrLater
  283.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  284. #endif    /* !SystemSevenOrLater */
  285.     {
  286.         return ( FSpOpenRF(spec, permission, refNum) );
  287.     }
  288. #if !SystemSevenOrLater
  289.     else
  290.     {
  291.         OSErr            result;
  292.         HParamBlockRec    pb;
  293.         
  294.         pb.ioParam.ioVRefNum = spec->vRefNum;
  295.         pb.fileParam.ioDirID = spec->parID;
  296.         pb.ioParam.ioNamePtr = (StringPtr) &(spec->name);
  297.         pb.ioParam.ioVersNum = 0;
  298.         pb.ioParam.ioPermssn = permission;
  299.         pb.ioParam.ioMisc = NULL;
  300.         result = PBHOpenRFSync(&pb);
  301.         *refNum = pb.ioParam.ioRefNum;
  302.         return ( result );
  303.     }
  304. #endif    /* !SystemSevenOrLater */
  305. }
  306.  
  307. /*****************************************************************************/
  308.  
  309. pascal    OSErr    FSpCreateCompat(const FSSpec *spec,
  310.                                 OSType creator,
  311.                                 OSType fileType,
  312.                                 ScriptCode scriptTag)
  313. {
  314.     OSErr            result;
  315.     UniversalFMPB    pb;
  316.     
  317.     /* There's no conditional to test for, so this test must be made */
  318.     if ( HasFSpCreateScriptSupportFix() )
  319.     {
  320.         return ( FSpCreate(spec, creator, fileType, scriptTag) );
  321.     }
  322.     /*    If FSpCreate isn't called, this code will be executed */
  323.     pb.hPB.fileParam.ioVRefNum = spec->vRefNum;
  324.     pb.hPB.fileParam.ioDirID = spec->parID;
  325.     pb.hPB.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  326.     pb.hPB.fileParam.ioFVersNum = 0;
  327.     result = PBHCreateSync(&(pb.hPB));
  328.     if ( result == noErr )
  329.     {
  330.         /* get info on created item */
  331.         pb.ciPB.hFileInfo.ioFDirIndex = 0;
  332.         result = PBGetCatInfoSync(&(pb.ciPB));
  333.         if ( result == noErr )
  334.         {
  335.             /* Set fdScript in FXInfo */
  336.             /* The negative script constants (smSystemScript, smCurrentScript, and smAllScripts) */
  337.             /* don't make sense on disk, so only use scriptTag if scriptTag >= smRoman */
  338.             /* (smRoman is 0). fdScript is valid if high bit is set (see IM-6, page 9-38) */
  339.             pb.ciPB.hFileInfo.ioFlXFndrInfo.fdScript = (scriptTag >= smRoman) ?
  340.                                                         ((char)scriptTag | (char)0x80) :
  341.                                                         (smRoman);
  342.             /* Set creator/fileType */
  343.             pb.ciPB.hFileInfo.ioFlFndrInfo.fdCreator = creator;
  344.             pb.ciPB.hFileInfo.ioFlFndrInfo.fdType = fileType;
  345.             /* Restore ioDirID field in pb which was changed by PBGetCatInfo */
  346.             pb.ciPB.hFileInfo.ioDirID = spec->parID;
  347.             result = PBSetCatInfoSync(&(pb.ciPB));
  348.         }
  349.     }
  350.     return ( result );
  351. }
  352.  
  353. /*****************************************************************************/
  354.  
  355. pascal    OSErr    FSpDirCreateCompat(const FSSpec *spec,
  356.                                    ScriptCode scriptTag,
  357.                                    long *createdDirID)
  358. {
  359. #if !SystemSevenOrLater
  360.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  361. #endif    /* !SystemSevenOrLater */
  362.     {
  363.         return ( FSpDirCreate(spec, scriptTag, createdDirID) );
  364.     }
  365. #if !SystemSevenOrLater
  366.     else
  367.     {
  368.         OSErr            result;
  369.         UniversalFMPB    pb;
  370.         
  371.         pb.hPB.fileParam.ioVRefNum = spec->vRefNum;
  372.         pb.hPB.fileParam.ioDirID = spec->parID;
  373.         pb.hPB.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  374.         result = PBDirCreateSync(&(pb.hPB));
  375.         *createdDirID = pb.hPB.fileParam.ioDirID;
  376.         if ( result == noErr )
  377.         {
  378.             /* get info on created item */
  379.             pb.ciPB.dirInfo.ioFDirIndex = 0;
  380.             pb.ciPB.dirInfo.ioDrDirID = spec->parID;
  381.             result = PBGetCatInfoSync(&(pb.ciPB));
  382.             if ( result == noErr )
  383.             {
  384.                 /* Set frScript in DXInfo */
  385.                 /* The negative script constants (smSystemScript, smCurrentScript, and smAllScripts) */
  386.                 /* don't make sense on disk, so only use scriptTag if scriptTag >= smRoman */
  387.                 /* (smRoman is 0). frScript is valid if high bit is set (see IM-6, page 9-38) */
  388.                 pb.ciPB.dirInfo.ioDrFndrInfo.frScript = (scriptTag >= smRoman) ?
  389.                                                             ((char)scriptTag | (char)0x80) :
  390.                                                             (smRoman);
  391.                 /* Restore ioDirID field in pb which was changed by PBGetCatInfo */
  392.                 pb.ciPB.dirInfo.ioDrDirID = spec->parID;            
  393.                 result = PBSetCatInfoSync(&(pb.ciPB));
  394.             }
  395.         }
  396.         return ( result );
  397.     }
  398. #endif    /* !SystemSevenOrLater */
  399. }
  400.  
  401. /*****************************************************************************/
  402.  
  403. pascal    OSErr    FSpDeleteCompat(const FSSpec *spec)
  404. {
  405. #if !SystemSevenOrLater
  406.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  407. #endif    /* !SystemSevenOrLater */
  408.     {
  409.         return ( FSpDelete(spec) );
  410.     }
  411. #if !SystemSevenOrLater
  412.     else
  413.     {
  414.         HParamBlockRec    pb;
  415.         
  416.         pb.ioParam.ioVRefNum = spec->vRefNum;
  417.         pb.fileParam.ioDirID = spec->parID;
  418.         pb.ioParam.ioNamePtr = (StringPtr) &(spec->name);
  419.         pb.ioParam.ioVersNum = 0;
  420.         return ( PBHDeleteSync(&pb) );
  421.     }
  422. #endif    /* !SystemSevenOrLater */
  423. }
  424.  
  425. /*****************************************************************************/
  426.  
  427. pascal    OSErr    FSpGetFInfoCompat(const FSSpec *spec,
  428.                                   FInfo *fndrInfo)
  429. {
  430. #if !SystemSevenOrLater
  431.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  432. #endif    /* !SystemSevenOrLater */
  433.     {
  434.         return ( FSpGetFInfo(spec, fndrInfo) );
  435.     }
  436. #if !SystemSevenOrLater
  437.     else
  438.     {
  439.         OSErr            result;
  440.         HParamBlockRec    pb;
  441.         
  442.         pb.fileParam.ioVRefNum = spec->vRefNum;
  443.         pb.fileParam.ioDirID = spec->parID;
  444.         pb.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  445.         pb.fileParam.ioFVersNum = 0;
  446.         pb.fileParam.ioFDirIndex = 0;
  447.         result = PBHGetFInfoSync(&pb);
  448.         *fndrInfo = pb.fileParam.ioFlFndrInfo;
  449.         return ( result );
  450.     }
  451. #endif    /* !SystemSevenOrLater */
  452. }
  453.  
  454. /*****************************************************************************/
  455.  
  456. pascal    OSErr    FSpSetFInfoCompat(const FSSpec *spec,
  457.                                   const FInfo *fndrInfo)
  458. {
  459. #if !SystemSevenOrLater
  460.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  461. #endif    /* !SystemSevenOrLater */
  462.     {
  463.         return ( FSpSetFInfo(spec, fndrInfo) );
  464.     }
  465. #if !SystemSevenOrLater
  466.     else
  467.     {
  468.         OSErr            result;
  469.         HParamBlockRec    pb;
  470.         
  471.         pb.fileParam.ioVRefNum = spec->vRefNum;
  472.         pb.fileParam.ioDirID = spec->parID;
  473.         pb.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  474.         pb.fileParam.ioFVersNum = 0;
  475.         pb.fileParam.ioFDirIndex = 0;
  476.         result = PBHGetFInfoSync(&pb);
  477.         if ( result == noErr )
  478.         {
  479.             pb.fileParam.ioFlFndrInfo = *fndrInfo;
  480.             pb.fileParam.ioDirID = spec->parID;
  481.             result = PBHSetFInfoSync(&pb);
  482.         }
  483.         return ( result );
  484.     }
  485. #endif    /* !SystemSevenOrLater */
  486. }
  487.  
  488. /*****************************************************************************/
  489.  
  490. pascal    OSErr    FSpSetFLockCompat(const FSSpec *spec)
  491. {
  492. #if !SystemSevenOrLater
  493.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  494. #endif    /* !SystemSevenOrLater */
  495.     {
  496.         return ( FSpSetFLock(spec) );
  497.     }
  498. #if !SystemSevenOrLater
  499.     else
  500.     {
  501.         HParamBlockRec    pb;
  502.         
  503.         pb.fileParam.ioVRefNum = spec->vRefNum;
  504.         pb.fileParam.ioDirID = spec->parID;
  505.         pb.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  506.         pb.fileParam.ioFVersNum = 0;
  507.         return ( PBHSetFLockSync(&pb) );
  508.     }
  509. #endif    /* !SystemSevenOrLater */
  510. }
  511.  
  512. /*****************************************************************************/
  513.  
  514. pascal    OSErr    FSpRstFLockCompat(const FSSpec *spec)
  515. {
  516. #if !SystemSevenOrLater
  517.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  518. #endif    /* !SystemSevenOrLater */
  519.     {
  520.         return ( FSpRstFLock(spec) );
  521.     }
  522. #if !SystemSevenOrLater
  523.     else
  524.     {
  525.         HParamBlockRec    pb;
  526.         
  527.         pb.fileParam.ioVRefNum = spec->vRefNum;
  528.         pb.fileParam.ioDirID = spec->parID;
  529.         pb.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  530.         pb.fileParam.ioFVersNum = 0;
  531.         return ( PBHRstFLockSync(&pb) );
  532.     }
  533. #endif    /* !SystemSevenOrLater */
  534. }
  535.  
  536. /*****************************************************************************/
  537.  
  538. pascal    OSErr    FSpRenameCompat(const FSSpec *spec,
  539.                                 ConstStr255Param newName)
  540. {
  541. #if !SystemSevenOrLater
  542.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  543. #endif    /* !SystemSevenOrLater */
  544.     {
  545.         return ( FSpRename(spec, newName) );
  546.     }
  547. #if !SystemSevenOrLater
  548.     else
  549.     {
  550.         HParamBlockRec    pb;
  551.         
  552.         pb.ioParam.ioVRefNum = spec->vRefNum;
  553.         pb.fileParam.ioDirID = spec->parID;
  554.         pb.ioParam.ioNamePtr = (StringPtr) &(spec->name);
  555.         pb.ioParam.ioVersNum = 0;
  556.         pb.ioParam.ioMisc = (Ptr) newName;
  557.         return ( PBHRenameSync(&pb) );
  558.     }
  559. #endif    /* !SystemSevenOrLater */
  560. }
  561.  
  562. /*****************************************************************************/
  563.  
  564. pascal    OSErr    FSpCatMoveCompat(const FSSpec *source,
  565.                                  const FSSpec *dest)
  566. {
  567. #if !SystemSevenOrLater
  568.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  569. #endif    /* !SystemSevenOrLater */
  570.     {
  571.         return ( FSpCatMove(source, dest) );
  572.     }
  573. #if !SystemSevenOrLater
  574.     else
  575.     {
  576.         CMovePBRec    pb;
  577.         
  578.         /* source and destination volume must be the same */
  579.         if ( source->vRefNum != dest->vRefNum )
  580.             return ( paramErr );
  581.         
  582.         pb.ioNamePtr = (StringPtr) &(source->name);
  583.         pb.ioVRefNum = source->vRefNum;
  584.         pb.ioDirID = source->parID;
  585.         pb.ioNewDirID = dest->parID;
  586.         pb.ioNewName = (StringPtr) &(dest->name);
  587.         return ( PBCatMoveSync(&pb) );
  588.     }
  589. #endif    /* !SystemSevenOrLater */
  590. }
  591.  
  592. /*****************************************************************************/
  593.  
  594. /* GenerateUniqueName generates a name that is unique in both dir1 and dir2 */
  595. /* on the specified volume. Ripped off from Feldman's code. */
  596.  
  597. #if !SystemSevenFiveOrLater
  598. static    OSErr    GenerateUniqueName(short volume,
  599.                                    long *startSeed,
  600.                                    long dir1,
  601.                                    long dir2,
  602.                                    StringPtr uniqueName)
  603. {
  604.     OSErr            error = noErr;
  605.     long            i;
  606.     CInfoPBRec        cinfo;
  607.     unsigned char    hexStr[16];
  608.     
  609.     for ( i = 0; i < 16; ++i )
  610.     {
  611.         if ( i < 10 )
  612.             hexStr[i] = 0x30 + i;
  613.         else
  614.             hexStr[i] = 0x37 + i;
  615.     }
  616.     
  617.     cinfo.hFileInfo.ioVRefNum = volume;
  618.     cinfo.hFileInfo.ioFDirIndex = 0;
  619.     cinfo.hFileInfo.ioNamePtr = uniqueName;
  620.  
  621.     while ( error != fnfErr )
  622.     {
  623.         (*startSeed)++;        
  624.         cinfo.hFileInfo.ioNamePtr[0] = 8;
  625.         for ( i = 1; i <= 8; i++ )
  626.         {
  627.             cinfo.hFileInfo.ioNamePtr[i] = hexStr[((*startSeed >> ((8-i)*4)) & 0xf)];
  628.         }
  629.         cinfo.hFileInfo.ioDirID = dir1;
  630.         error = fnfErr;
  631.         for ( i = 1; i <= 2; i++ )
  632.         {
  633.             error = error & PBGetCatInfoSync(&cinfo);
  634.             cinfo.hFileInfo.ioDirID = dir2;
  635.             if ( (error != fnfErr) && (error != noErr) )
  636.                 return ( error );
  637.         }
  638.     }
  639.     return ( noErr );
  640. }
  641. #endif    /* !SystemSevenFiveOrLater */
  642.  
  643. /*****************************************************************************/
  644.  
  645. pascal    OSErr    FSpExchangeFilesCompat(const FSSpec *source,
  646.                                        const FSSpec *dest)
  647. {
  648. #if !SystemSevenOrLater
  649.     if ( FSHasFSSpecCalls() )
  650. #endif    /* !SystemSevenOrLater */
  651.     {
  652. #if !SystemSevenFiveOrLater
  653.         if ( HasFSpExchangeFilesCompatibilityFix() )
  654. #endif    /* !SystemSevenFiveOrLater */
  655.         {
  656.             return ( FSpExchangeFiles(source, dest) );
  657.         }
  658.     }
  659. #if !SystemSevenFiveOrLater
  660.     /*    If FSpExchangeFiles isn't called, this code will be executed */
  661.     {
  662.         HParamBlockRec            pb;
  663.         CInfoPBRec                catInfoSource, catInfoDest;
  664.         OSErr                    result, result2;
  665.         Str31                    unique1, unique2;
  666.         StringPtr                unique1Ptr, unique2Ptr, swapola;
  667.         GetVolParmsInfoBuffer    volInfo;
  668.         long                    theSeed, temp;
  669.         
  670.         /* Make sure the source and destination are on the same volume */
  671.         if ( source->vRefNum != dest->vRefNum )
  672.         {
  673.             result = diffVolErr;
  674.             goto errorExit3;
  675.         }
  676.         
  677.         /* Try PBExchangeFiles first since it preserves the file ID reference */
  678.         pb.fidParam.ioNamePtr = (StringPtr) &(source->name);
  679.         pb.fidParam.ioVRefNum = source->vRefNum;
  680.         pb.fidParam.ioDestNamePtr = (StringPtr) &(dest->name);
  681.         pb.fidParam.ioDestDirID = dest->parID;
  682.         pb.fidParam.ioSrcDirID = source->parID;
  683.     
  684.         result = PBExchangeFilesSync(&pb);
  685.     
  686.         /* Note: The compatibility case won't work for files with *Btree control blocks. */
  687.         /* Right now the only *Btree files are created by the system. */
  688.         if ( result != noErr )
  689.         {
  690.             pb.ioParam.ioNamePtr = NULL;
  691.             pb.ioParam.ioBuffer = (Ptr) &volInfo;
  692.             pb.ioParam.ioReqCount = sizeof(volInfo);
  693.             result2 = PBHGetVolParmsSync(&pb);
  694.             
  695.             /* continue if volume has no fileID support (or no GetVolParms support) */
  696.             if ( (result2 == noErr) && hasFileIDs(volInfo) )
  697.                 goto errorExit3;
  698.     
  699.             /* Get the catalog information for each file */
  700.             /* and make sure both files are *really* files */
  701.             catInfoSource.hFileInfo.ioVRefNum = source->vRefNum;
  702.             catInfoSource.hFileInfo.ioFDirIndex = 0;
  703.             catInfoSource.hFileInfo.ioNamePtr = (StringPtr) &(source->name);
  704.             catInfoSource.hFileInfo.ioDirID = source->parID;
  705.             catInfoSource.hFileInfo.ioACUser = 0; /* ioACUser used to be filler2 */
  706.             result = PBGetCatInfoSync(&catInfoSource);
  707.             if ( result != noErr )
  708.                 goto errorExit3;
  709.             if ( (catInfoSource.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  710.             {
  711.                 result = notAFileErr;
  712.                 goto errorExit3;
  713.             }
  714.             
  715.             catInfoDest.hFileInfo.ioVRefNum = dest->vRefNum;
  716.             catInfoDest.hFileInfo.ioFDirIndex = 0;
  717.             catInfoDest.hFileInfo.ioNamePtr = (StringPtr) &(dest->name);
  718.             catInfoDest.hFileInfo.ioDirID = dest->parID;
  719.             catInfoDest.hFileInfo.ioACUser = 0; /* ioACUser used to be filler2 */
  720.             result = PBGetCatInfoSync(&catInfoDest);
  721.             if ( result != noErr )
  722.                 goto errorExit3;
  723.             if ( (catInfoDest.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  724.             {
  725.                 result = notAFileErr;
  726.                 goto errorExit3;
  727.             }
  728.             
  729.             /* generate 2 filenames that are unique in both directories */
  730.             theSeed = 0x64666A6C;    /* a fine unlikely filename */
  731.             unique1Ptr = (StringPtr)&unique1;
  732.             unique2Ptr = (StringPtr)&unique2;
  733.             
  734.             result = GenerateUniqueName(source->vRefNum, &theSeed, source->parID, dest->parID, unique1Ptr);
  735.             if ( result != noErr )
  736.                 goto errorExit3;
  737.     
  738.             GenerateUniqueName(source->vRefNum, &theSeed, source->parID, dest->parID, unique2Ptr);
  739.             if ( result != noErr )
  740.                 goto errorExit3;
  741.     
  742.             /* rename source to unique1 */
  743.             pb.fileParam.ioNamePtr = (StringPtr) &(source->name);
  744.             pb.ioParam.ioMisc = (Ptr) unique1Ptr;
  745.             pb.ioParam.ioVersNum = 0;
  746.             result = PBHRenameSync(&pb);
  747.             if ( result != noErr )
  748.                 goto errorExit3;
  749.             
  750.             /* rename dest to unique2 */
  751.             pb.ioParam.ioMisc = (Ptr) unique2Ptr;
  752.             pb.ioParam.ioVersNum = 0;
  753.             pb.fileParam.ioNamePtr = (StringPtr) &(dest->name);
  754.             pb.fileParam.ioDirID = dest->parID;
  755.             result = PBHRenameSync(&pb);
  756.             if ( result != noErr )
  757.                 goto errorExit2;    /* back out gracefully by renaming unique1 back to source */
  758.                 
  759.             /* If files are not in same directory, swap their locations */
  760.             if ( source->parID != dest->parID )
  761.             {
  762.                 /* move source file to dest directory */
  763.                 pb.copyParam.ioNamePtr = unique1Ptr;
  764.                 pb.copyParam.ioNewName = NULL;
  765.                 pb.copyParam.ioNewDirID = dest->parID;
  766.                 pb.copyParam.ioDirID = source->parID;
  767.                 result = PBCatMoveSync((CMovePBPtr) &pb);
  768.                 if ( result != noErr )
  769.                     goto errorExit1;    /* back out gracefully by renaming both files to original names */
  770.                 
  771.                 /* move dest file to source directory */
  772.                 pb.copyParam.ioNamePtr = unique2Ptr;
  773.                 pb.copyParam.ioNewDirID = source->parID;
  774.                 pb.copyParam.ioDirID = dest->parID;
  775.                 result = PBCatMoveSync((CMovePBPtr) &pb);
  776.                 if ( result != noErr)
  777.                 {
  778.                     /* life is very bad.  We'll at least try to move source back */
  779.                     pb.copyParam.ioNamePtr = unique1Ptr;
  780.                     pb.copyParam.ioNewName = NULL;
  781.                     pb.copyParam.ioNewDirID = source->parID;
  782.                     pb.copyParam.ioDirID = dest->parID;
  783.                     (void) PBCatMoveSync((CMovePBPtr) &pb);    /* ignore errors */
  784.                     goto errorExit1;    /* back out gracefully by renaming both files to original names */
  785.                 }
  786.             }
  787.             
  788.             /* Make unique1Ptr point to file in source->parID */
  789.             /* and unique2Ptr point to file in dest->parID */
  790.             /* This lets us fall through to the rename code below */
  791.             swapola = unique1Ptr;
  792.             unique1Ptr = unique2Ptr;
  793.             unique2Ptr = swapola;
  794.     
  795.             /* At this point, the files are in their new locations (if they were moved) */
  796.             /* Source is named Unique1 (name pointed to by unique2Ptr) and is in dest->parID */
  797.             /* Dest is named Unique2 (name pointed to by unique1Ptr) and is in source->parID */
  798.             /* Need to swap attributes except mod date and swap names */
  799.     
  800.             /* swap the catalog info by re-aiming the CInfoPB's */
  801.             catInfoSource.hFileInfo.ioNamePtr = unique1Ptr;
  802.             catInfoDest.hFileInfo.ioNamePtr = unique2Ptr;
  803.             
  804.             catInfoSource.hFileInfo.ioDirID = source->parID;
  805.             catInfoDest.hFileInfo.ioDirID = dest->parID;
  806.             
  807.             /* Swap the original mod dates with each file */
  808.             temp = catInfoSource.hFileInfo.ioFlMdDat;
  809.             catInfoSource.hFileInfo.ioFlMdDat = catInfoDest.hFileInfo.ioFlMdDat;
  810.             catInfoDest.hFileInfo.ioFlMdDat = temp;
  811.             
  812.             /* Here's the swap (ignore errors) */
  813.             (void) PBSetCatInfoSync(&catInfoSource); 
  814.             (void) PBSetCatInfoSync(&catInfoDest);
  815.             
  816.             /* rename unique2 back to dest */
  817. errorExit1:
  818.             pb.ioParam.ioMisc = (Ptr) &(dest->name);
  819.             pb.ioParam.ioVersNum = 0;
  820.             pb.fileParam.ioNamePtr = unique2Ptr;
  821.             pb.fileParam.ioDirID = dest->parID;
  822.             (void) PBHRenameSync(&pb);    /* ignore errors */
  823.     
  824.             /* rename unique1 back to source */
  825. errorExit2:
  826.             pb.ioParam.ioMisc = (Ptr) &(source->name);
  827.             pb.ioParam.ioVersNum = 0;
  828.             pb.fileParam.ioNamePtr = unique1Ptr;
  829.             pb.fileParam.ioDirID = source->parID;
  830.             (void) PBHRenameSync(&pb); /* ignore errors */
  831.         }
  832. errorExit3: { /* null statement */ }
  833.         return ( result );
  834.     }
  835. #endif    /* !SystemSevenFiveOrLater */
  836. }
  837.  
  838. /*****************************************************************************/
  839.  
  840. /* 
  841. **    Resource Manager FSp calls
  842. */
  843.  
  844. /*****************************************************************************/
  845.  
  846. pascal    short    FSpOpenResFileCompat(const FSSpec *spec,
  847.                                      SignedByte permission)
  848. {
  849. #if !SystemSevenOrLater
  850.     if ( FSHasFSSpecCalls() || QTHasFSSpecCalls() )
  851. #endif    /* !SystemSevenOrLater */
  852.     {
  853.         return ( FSpOpenResFile(spec, permission) );
  854.     }
  855. #if !SystemSevenOrLater
  856.     else
  857.     {
  858.         return ( HOpenResFile(spec->vRefNum, spec->parID, spec->name, permission) );
  859.     }
  860. #endif    /* !SystemSevenOrLater */
  861. }
  862.  
  863. /*****************************************************************************/
  864.  
  865. pascal    void    FSpCreateResFileCompat(const FSSpec *spec,
  866.                                        OSType creator,
  867.                                        OSType fileType,
  868.                                        ScriptCode scriptTag)
  869. {    
  870.     if ( HasFSpCreateScriptSupportFix() )
  871.     {
  872.         FSpCreateResFile(spec, creator, fileType, scriptTag);
  873.         return;
  874.     }
  875.     /*    If FSpCreateResFile isn't called, this code will be executed */
  876.     {
  877.         OSErr            result;
  878.         CInfoPBRec        pb;
  879.         
  880.         HCreateResFile(spec->vRefNum, spec->parID, spec->name);
  881.         if ( ResError() == noErr )
  882.         {
  883.             /* get info on created item */
  884.             pb.hFileInfo.ioVRefNum = spec->vRefNum;
  885.             pb.hFileInfo.ioDirID = spec->parID;
  886.             pb.hFileInfo.ioNamePtr = (StringPtr) &(spec->name);
  887.             pb.hFileInfo.ioFDirIndex = 0;
  888.             result = PBGetCatInfoSync(&pb);
  889.             if ( result == noErr )
  890.             {
  891.                 /* Set fdScript in FXInfo */
  892.                 /* The negative script constants (smSystemScript, smCurrentScript, and smAllScripts) */
  893.                 /* don't make sense on disk, so only use scriptTag if scriptTag >= smRoman */
  894.                 /* (smRoman is 0). fdScript is valid if high bit is set (see IM-6, page 9-38) */
  895.                 pb.hFileInfo.ioFlXFndrInfo.fdScript = (scriptTag >= smRoman) ?
  896.                                                         ((char)scriptTag | (char)0x80) :
  897.                                                         (smRoman);
  898.                 /* Set creator/fileType */
  899.                 pb.hFileInfo.ioFlFndrInfo.fdCreator = creator;
  900.                 pb.hFileInfo.ioFlFndrInfo.fdType = fileType;
  901.                 
  902.                 /* Restore ioDirID field in pb which was changed by PBGetCatInfo */
  903.                 pb.hFileInfo.ioDirID = spec->parID;
  904.                 result = PBSetCatInfoSync(&pb);
  905.             }
  906.             /* Set ResErr low memory global to result */
  907.             LMSetResErr(result);
  908.         }
  909.         return;
  910.     }
  911. }
  912.  
  913. /*****************************************************************************/
  914.